package se.emilsjolander.sprinkles;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

class DbOpenHelper extends SQLiteOpenHelper {
//    private int baseVersion;
	private int currentVersion;

    protected DbOpenHelper(Context context, String databaseName, int version) {
    	super(context, databaseName, null, version);
//        super(context, databaseName, null, Sprinkles.sInstance.mMigrations.size() + baseVersion);
//        this.baseVersion = baseVersion;
    	this.currentVersion = version;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        executeMigrations(db, 0, currentVersion);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        executeMigrations(db, oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.execSQL("PRAGMA foreign_keys=ON;");
    }

    private void executeMigrations(SQLiteDatabase db, int oldVersion, int newVersion) {
//        for (int i = oldVersion; i < newVersion; i++) {
//            Sprinkles.sInstance.mMigrations.get(i).execute(db);
//        }
    	
    	//暂时不清楚上面为何 migration 要有多个（菜鸟～），这里数据库只做一个配置吧
    	//2015-01-13 Even
    	Sprinkles.sInstance.mMigrations.get(0).execute(db, oldVersion, newVersion);
    }
}
